package com.service.business.rabbitmq.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.business.rabbitmq.service.SysMqRecordService;
import com.service.core.exception.CommonException;
import com.service.dao.enums.CommonStatusEnums;
import com.service.dao.enums.MqMsgVo;
import com.service.dao.enums.SysMqTopicEnum;
import com.service.dao.mapper.SysMqRecordMapper;
import com.service.dao.model.SysMqRecord;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;


@Service("sysMqRecordService")
public class SysMqRecordServiceImpl extends ServiceImpl<SysMqRecordMapper, SysMqRecord> implements SysMqRecordService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMqMsgRecord(SysMqTopicEnum topic, MqMsgVo mqMsgVo) {

        SysMqRecord record  =  SysMqRecord.builder()
                .mqTopic(topic.getName())
                .sendTime(new Date())
                .sendData(JSON.toJSONString(mqMsgVo))
                .recordStatus(CommonStatusEnums.INVALID.getCode())
                .build();
        save(record);

        mqMsgVo.setMsgId(record.getRecordId());
    }

    @Override
    public void confirmReceiveMsh(Long recordId) {
        if(ObjectUtils.isEmpty(recordId)){
            throw new CommonException("消息id错误,非法消息");
        }
        updateById(SysMqRecord.builder().recordId(recordId)
                .recordStatus(CommonStatusEnums.ACTIVE.getCode())
                .confirmTime(new Date())
                .build());
    }
}
